package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.entity.StudentParam;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     * @param studentParam
     * @return
     */
    @Override
    public PageBean page(StudentParam studentParam){
        PageHelper.startPage(studentParam.getPage(), studentParam.getPageSize());
        //调用mapper查询数据
        List<Student> studentList=studentMapper.page(studentParam);
        //强行转化
        Page p= (Page) studentList;
        //封装PageBean
        return new PageBean(p.getTotal(),p.getResult());
    }

    /**
     * 删除学生
     * @param ids
     */
    @Override
    public void delete(Integer[] ids){
        studentMapper.delete(ids);
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void save(Student student){
        //补充数据
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        //添加数据
        studentMapper.save(student);
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id){

        Student student=studentMapper.getById(id);
        return student;
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void update(Student student){
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 添加违纪次数
      * @param id
     * @param score
     */
    @Override
    public void add(Integer id,Integer score){

        studentMapper.add(id,score);
    }
}
